package cat.music.ut.comm.ext

import android.content.Intent
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver
import cat.music.ut.R
import cat.music.ut.component.log
import me.jingbin.library.BuildConfig
import me.jingbin.library.ByRecyclerView

class Ext {
}

fun Double.format2(): String {
    val decimalFormat = java.text.DecimalFormat("#.##")
    return decimalFormat.format(this)
}

/**
 * 列表空数据展示的内容
 */
fun ByRecyclerView.setEmptyView() {
    setEmptyView(R.layout.view_empty)
    adapter?.registerAdapterDataObserver(object : AdapterDataObserver() {
        override fun onChanged() {
            super.onChanged()
            setEmptyViewEnabled(adapter?.itemCount == 0)
            "registerAdapterDataObserver".log()
        }
    })
}

fun Fragment.startActivity(cls: Class<*>, block: Intent.() -> Unit = {}) {
    val intent = Intent(requireActivity(), cls)
    block.invoke(intent)
    startActivity(intent)
}
fun debug(block: () -> Unit){
    if(BuildConfig.DEBUG){
        block.invoke()
    }
}
fun release(block: () -> Unit){
    if(!BuildConfig.DEBUG){
        block.invoke()
    }
}